#include "frame_behaviour.h"
#include "frame.h"
#include "frame_controller.h"

FrameBehaviour::FrameBehaviour()
    : _isDone(false)
{
}
    
FrameBehaviour::~FrameBehaviour()
{
}

bool FrameBehaviour::isDone() const
{
    return _isDone;
}

bool FrameBehaviour::isNeedRenewPins() const
{
    return false;
}
        
/*
void FrameBehaviourSimple::fired(int knockedPinCount, int pinsLeft, int throwCount)
{
    if (pinsLeft == 0 || throwCount == 2)
    {
        _isDone = true;
    }
}*/

void FrameBehaviourSimple::fired(Frame* frame, int knockedPinCount)
{
    if (frame->getPinsLeft() == 0 || frame->getThrowCount() == 2)
    {
        _isDone = true;
    }
}


FrameBehaviourLast::FrameBehaviourLast()
    : _extraThrows(0)
{
}
    
void FrameBehaviourLast::fired(Frame* frame, int knockedPinCount)
{
    //std::cout << "fire from last frame" << std::endl;
        
    int pinsLeft   = frame->getPinsLeft();
    int throwCount = frame->getThrowCount();
    
    std::cout << "throwCount: " << throwCount << std::endl;
    
    if (knockedPinCount == PINS_COUNT) // strike
    {
    
    }
    else if (pinsLeft == 0) // spare
    {
    
    }
    else
    {
    
    }
    
    

    if (throwCount == 2 + _extraThrows)
    {
        std::cout << "DONE last frame" << std::endl;
        _isDone = true;
        return;
    }
    else if (throwCount == 1 && knockedPinCount == PINS_COUNT)   //  strike in first turn
    {
        std::cout << "STRIKE ------ " << std::endl;
        // renew frame
        _extraThrows = 2;
    }
    else if (throwCount == 2 && pinsLeft == 0 && knockedPinCount != PINS_COUNT) // spare in second turn
    {
        // renew frame
        _extraThrows = 1;
    }
    
    if (throwCount >= 2)
    {
        //  need renew pins
        std::cout << "----- extra frame update --------- " << std::endl;
        FrameController::getInstance().updateFrameReq();
        
    }
}

